package com.example.mybatis.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.example.mybatis.bean.AnnualPlan;

import com.example.mybatis.mapper.AnnualPlanMapper;
import com.example.mybatis.service.AnnualPlanService;
import com.example.mybatis.utils.ResultUtil;
import com.example.mybatis.utils.validator.DataValidator;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class AnnualPlanImpl implements AnnualPlanService {
    @Resource
    AnnualPlanMapper annualPlanMapper;
    @Override
    public ResultUtil selectList(String theme) {
        List<AnnualPlan> dataList = annualPlanMapper.selectList(theme);
        return ResultUtil.ok().put("dataList", dataList);
    }

    @Override
    public ResultUtil save(AnnualPlan annualPlan) {
        DataValidator.validator(annualPlan);
        annualPlanMapper.insertBatch(CollUtil.newArrayList(annualPlan));
        return ResultUtil.ok();
    }

    @Override
    public ResultUtil update(AnnualPlan annualPlan) {
        DataValidator.validator(annualPlan);
        annualPlanMapper.update(annualPlan);
        return ResultUtil.ok();
    }

    @Override
    public ResultUtil removeById(Integer id) {
        annualPlanMapper.deleteById(id);
        return ResultUtil.ok();
    }

    @Override
    public ResultUtil getById(Integer id) {
        AnnualPlan annualPlan = null;
        // 修改根据id查询原值
        if (id != null) {
            annualPlan = annualPlanMapper.selectById(id);
        }
        return ResultUtil.ok().put("annualPlan",annualPlan);

    }
}
